{include file="common/mini-header" title="权限节点"}
<div class="layui-body">
	<!-- 内容主体区域 -->
	
	<div id="ruleTree"></div>
	
	<div class="layui-form-item" style="margin-top: 50px;">
		<button class="layui-btn" lay-submit lay-filter="saveBtn">提交</button>
	</div>

</div>
<script>
	var roleId = '{$roleInfo["id"]}'
	layui.use(['tree', 'form'], function() {
		var $ = layui.jquery;
		var layer = layui.layer;
		var tree = layui.tree;
		var form = layui.form;

		var rule = tree.render({
			showCheckbox: true,
			elem: '#ruleTree',
			id: 'ruleTree',
			showLine: true,
			data: [{
				"id": -1,
				"spread": true, 
				"title": "权限节点",
				"children": {php}echo json_encode($ruleJson);{/php}}],
		});
	 
		//监听提交
		form.on('submit(saveBtn)', function(data) { 
			var ruleIds = getTreeId(tree.getChecked('ruleTree'));
			$.post('./selectRule', {
				'roleId': roleId,
				'ruleIds': ruleIds
			}, function(rs) {
				if (rs.code == 1) {
					layer.msg(rs.msg)
					setTimeout(function() {
						parent.location.reload();
					}, 1000)
				} else {
					layer.alert(rs.msg)
				}
			})
			return false;
		});
	});

	function getTreeId(datas) {
		var ids = ''
		for (i in datas) {
			var data = datas[i] 
			if (data.id) {
				ids += data.id + ',';
				if (data.children) {
					ids += getTreeId(data.children);
				}
			}
		}
		return ids;
	}
</script>
{include file="common/footer"}
